//

#import "AM_SettingViewController.h"
#import "AM_SettingTableViewCell.h"
#import "AM_DeleteHistoryViewController.h"

@interface AM_SettingViewController ()<UITableViewDelegate,UITableViewDataSource,SettingTableViewDelegate>{
    NSFileManager *files;
    NSString *path;
}
/** <#Description#>*/
@property (nonatomic, strong) UITableView *tableView;
/** <#Description#>*/
@property (nonatomic, strong) NSArray *dataArray;
/** 是否展开 yes-展开*/
@property (nonatomic, assign) BOOL  isShow;

@end

@implementation AM_SettingViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationItem.title = @"Setting";
    
    
    [self.view addSubview:self.tableView];
}

//#pragma mark ----------------- tableViewDelegate ----------------
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    static NSString *cellIdentifier = @"cellIdentifier";
    static NSString *cellIdentifier2 = @"cellIdentifier2";
    if (indexPath.section == 0) {
        AM_SettingTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier];
        if (!cell) {
            cell = [[AM_SettingTableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier];
        }
        cell.delegate = self;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.titleLabel.text = self.dataArray[indexPath.section];
        if (_isShow) {
            CGAffineTransform transform = CGAffineTransformMakeRotation(90 * M_PI/180.0);
            cell.arrowButton.transform = transform;
            cell.detailLabel.hidden = NO;
        }else{
            cell.detailLabel.hidden = YES;
        }
        return cell;
    }
    //
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:cellIdentifier2];
    if (!cell) {
        cell = [[UITableViewCell alloc]initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:cellIdentifier2];
    }
    cell.selectionStyle = UITableViewCellSelectionStyleNone;
    if (indexPath.section == 2) {
        cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
    }
    cell.textLabel.text = self.dataArray[indexPath.section];
    if (indexPath.section == 1) {
        path = [NSSearchPathForDirectoriesInDomains(NSCachesDirectory,NSUserDomainMask, YES) objectAtIndex:0];
        files= [NSFileManager defaultManager];
        NSArray *array = [files subpathsAtPath:path];
        NSString *caches=[NSString stringWithFormat:@"%0.2fM",(float)([array count])/30];
        DLog(@"===--file----%@",caches);
        cell.detailTextLabel.text = caches;
    }
    
    return cell;
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section {
    return kRelativeHeight(10);
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section {
    return nil;
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0) {
        if (_isShow) {
            return kRelativeHeight(120);
        }else
            return kRelativeHeight(50);
    }
    return kRelativeHeight(50);
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    //
    if (indexPath.section == 1) {
        [self removefile];
    }else if (indexPath.section == 0){
        [self setCellButton:nil];
    }else{
        AM_DeleteHistoryViewController *vc = [[AM_DeleteHistoryViewController alloc]init];
        [self.navigationController pushViewController:vc animated:YES];
    }
    
}

#pragma mark -----------------  自定义方法 ----------------
- (void)removefile {
    //
    [files removeItemAtPath:path error:nil];//删除路径里面的文件
    [_tableView reloadSections:[[NSIndexSet alloc]initWithIndex:1] withRowAnimation:UITableViewRowAnimationAutomatic];
}
//MARK:
- (void)setCellButton:(UIButton*)button {
    //
    if (_isShow) {
        _isShow = NO;
    }else
        _isShow = YES;
    //
    [_tableView reloadSections:[[NSIndexSet alloc]initWithIndex:0] withRowAnimation:UITableViewRowAnimationAutomatic];
}


//
#pragma mark ----------------- 懒加载 ----------------
- (UITableView *)tableView {
    if (!_tableView) {
        _tableView = [[UITableView alloc]initWithFrame:CGRectMake(0, UI_NAV_BAR_HEIGHT, kMainScreenWidth, kMainScreenHeight - UI_NAV_BAR_HEIGHT) style:UITableViewStylePlain];
        _tableView.dataSource = self;
        _tableView.delegate   = self;
        _tableView.tableFooterView = [UIView new];
    }
    return _tableView;
}
- (NSArray *)dataArray {
    if (!_dataArray) {
        _dataArray = @[@"AboutMemorandum",@"ClearCache",@"DeleteRecord"];
    }return _dataArray;
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

@end
